package ext;


import org.junit.Test;

public class IpParseInt {

    public static long parseIpToInt(String ip) {
        String[] ss = ip.split("\\.");
        int idx = 0;
        long res = 0L;
        for (int i = ss.length - 1; i >= 0; i--) {
            int t = Integer.parseInt(ss[i]);
            for (int j = 0; j < 8; j++) {
                if ((t & 1) == 1) {
                    res += (long)(Math.pow(2, idx));
                }
                idx++;
                t>>>=1;
            }
        }
        return res;
    }

    public static long parseIpToInt2(String ip) {
        String[] ss = ip.split("\\.");
        int idx = 0;
        long res = 0L;
        for (int i = ss.length - 1; i >= 0; i--) {
            long t = Long.parseLong(ss[i]);
            res = res | (t << idx);
            idx += 8;
        }
        return res;
    }

    public static String intParseIp(int num) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int t = 0, idx = 0;
            for (int j = 0; j < 8; j++) {
                if ((num & 1) == 1) {
                    t += (int)(Math.pow(2, idx));
                }
                idx++;
                num >>>= 1;
            }
            sb.insert(0, t);
            sb.insert(0, '.');
        }
        return sb.substring(1);
    }
    

    public static String intParseIp(long num) {
        StringBuilder sb = new StringBuilder();
        long tar = 0;
        for (int i = 0; i < 8; i++) {
            tar += (long)(Math.pow(2, i));
        }
        for (int i = 0; i < 4; i++) {
            long t = tar & num;
            sb.insert(0, t);
            sb.insert(0, '.');
            num >>>= 8;
        }
        return sb.substring(1);
    }


    @Test
    public void test() {
        // System.out.println(IpParseInt.in?ParseIp(IpParseInt.parseIpToInt("10.0.3.193")));
        System.out.println(IpParseInt.parseIpToInt2("234.237.37.104"));
        System.out.println(IpParseInt.parseIpToInt("234.237.37.104"));
        System.out.println(intParseIp(IpParseInt.parseIpToInt2("234.237.37.104")));
    }

    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // sc.close();
        System.out.println(parseIpToInt("10.10.3.193"));
        System.out.println("a");
        System.out.println("\n".strip());
        System.out.println("b");
    }
    
}
